import random
# import json
RECORDS=[]
INDEXU={}
INDEXF={}
ALLUADDR=[]
UADDR2FNAME={}
TYPE=0
FNAME=1
UADDR=2
TMPSTAMP=3
BLKNUM=4
Q1STEP=75
Q2STEP=9
Q3STEP=1
# 调用fill，莫调用load
def load():
    global RECORDS
    fname='D:\Program_Files\Geth\YanMengAudit\YanMengAuditRecord.txt'
    index=0
    with open(fname,'r')as fr:
        for lines in fr:
            x1,x2=lines.split(';')
            oneRecord=x1.split(',')
            # print(oneRecord)
            if(len(oneRecord)>1):
                RECORDS.append([int(oneRecord[0]),oneRecord[1],oneRecord[2],oneRecord[3],int(oneRecord[4])])
                if oneRecord[UADDR] not in INDEXU:
                    INDEXU[oneRecord[UADDR]]=[index]
                else:
                    INDEXU[oneRecord[UADDR]].append(index)
                if oneRecord[FNAME] not in INDEXF:
                    INDEXF[oneRecord[FNAME]]=[index]
                else:
                    INDEXF[oneRecord[FNAME]].append(index)
                index=index+1
def queryUFTT(user,filename,blk1,blk2):
    '''
    查询用户的某个文件在某段时间内被审计的记录。
    注意，为了方便使用，T被使用块号代替。
    [blk1,blk2)，左闭右开。
    '''
    accessedBlks={-1}
    potentialRecordsU=[RECORDS[i] for i in INDEXU[user]]
    for record in potentialRecordsU:
        if record[FNAME]==filename:
            if record[BLKNUM]>=blk1 and record[BLKNUM]<blk2:
                accessedBlks.add(record[BLKNUM])
    accessedBlks.remove(-1)
    return accessedBlks

def queryUTT(user,blk1,blk2):
    accessedBlks={-1}
    potentialRecordsU=[RECORDS[i] for i in INDEXU[user]]
    for record in potentialRecordsU:
        if record[BLKNUM]>=blk1 and record[BLKNUM]<blk2:
            accessedBlks.add(record[BLKNUM])
    accessedBlks.remove(-1)
    return accessedBlks

def queryTT(blk1,blk2):
    accessedBlks={-1}
    index=0
    recordsLen=len(RECORDS)
    while(index<recordsLen and RECORDS[index][BLKNUM]<blk1):
        index=index+1
    
    while(index<recordsLen and RECORDS[index][BLKNUM]>=blk1 and RECORDS[index][BLKNUM]<blk2):
        accessedBlks.add(RECORDS[index][BLKNUM])
        index=index+1
    accessedBlks.remove(-1)
    return accessedBlks


def fill():
    load()
    for keys in INDEXU:
        ALLUADDR.append(keys)
        UADDR2FNAME[keys]=[]
        for recordIndex in INDEXU[keys]:
            fname=RECORDS[recordIndex][FNAME]
            if fname not in UADDR2FNAME[keys]:
                UADDR2FNAME[keys].append(fname)

def queryrandom(endid):
    randquery=random.randint(1,3)
    if randquery==1:
        randUsr=random.randint(0,len(ALLUADDR)-1)
        randFn=random.randint(0,len(UADDR2FNAME[ALLUADDR[randUsr]])-1)
        access_blks=queryUFTT(ALLUADDR[randUsr],UADDR2FNAME[ALLUADDR[randUsr]][randFn],endid-Q1STEP,endid)
    elif randquery==2:
        randUsr=random.randint(0,len(ALLUADDR)-1)
        access_blks=queryUTT(ALLUADDR[randUsr],endid-Q2STEP,endid)
    else:
        access_blks=queryTT(endid-Q3STEP,endid)
    return access_blks
        


def getRECORDS():
    return RECORDS
def getALLUADDR():
    return ALLUADDR
def getUADDR2FNAME():
    return UADDR2FNAME
# print(len(ALLUADDR))
# print([len(UADDR2FNAME[keys]) for keys in ALLUADDR])
# print(sum([len(UADDR2FNAME[keys]) for keys in ALLUADDR]))
# startblknum=RECORDS[0][BLKNUM]
# endblknum=RECORDS[-1][BLKNUM]
# Q1Step=10
# Q2Step=1
# Q3Step=1
# blknumber1=[]
# blknumber2=[]
# blknumber3=[]
# TESTRANGE=400
# CICLES=1
# for j in range(0,CICLES):
#     for i in range(startblknum,startblknum+TESTRANGE):
#         randUsr=random.randint(0,len(ALLUADDR)-1)
#         randFn=random.randint(0,len(UADDR2FNAME[ALLUADDR[randUsr]])-1)
#         res=queryUFTT(ALLUADDR[randUsr],UADDR2FNAME[ALLUADDR[randUsr]][randFn],i,i+Q1Step)
#         blknumber1.append(len(res))
#         randUsr=random.randint(0,len(ALLUADDR)-1)
#         res=queryUTT(ALLUADDR[randUsr],i,i+Q2Step)
#         blknumber2.append(len(res))
#         res=queryTT(i,i+Q3Step)
#         # print(len(res))
#         blknumber3.append(len(res))
# with open('f1.txt','w')as fw:
#     # print(json.dumps(blknumber1),file=fw)
#     print(json.dumps([sum(blknumber1),sum(blknumber1)/len(blknumber1)]),file=fw)
#     # print(json.dumps(blknumber2),file=fw)
#     print(json.dumps([sum(blknumber2),sum(blknumber2)/len(blknumber2)]),file=fw)
#     # print(json.dumps(blknumber3),file=fw)
#     print(json.dumps([sum(blknumber3),sum(blknumber3)/len(blknumber3)]),file=fw)
#     print(Q1Step,Q2Step,Q3Step,file=fw)
